using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using vs.Core;
using vs.Authentication;

namespace Forum
{
    public partial class reply_quote : System.Web.UI.UserControl
    {
        private void RegistJavascriptEditor()
        {
            SmileController smile = new SmileController();

            string jsInsertSmile = smile.js_insert_smilie(txtContent.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jsInsertSmile", jsInsertSmile);
            lblSmilies.Text = smile.list_images_smilies();

            string jsInsertEditor = smile.js_insert_editor(txtContent.ClientID);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jsInsertEditor", jsInsertEditor);
            lblEditor.Text = smile.list_images_editor();
        }

        private void LoadStaffList()
        {
            if (Authenticate.IsEditor)
            {
                ddlStaff.Visible = true;
                UserDB.FillStaff(ddlStaff);
                RegisterScriptIMG();
            }
        }

        private void RegisterScriptIMG()
        {
            string js = @"
<script language=javascript>
    function popup2 (name,width,height) { 
         var options = ""toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1, width=""+width+"",height=""+height; 
         Cal2=window.open(name,""popup"",options); 
    }

    function SetImage(val)
    {
        val = '[img]' + val + '[/img]';
        var content = document.getElementById('" + txtContent.ClientID + @"');
        if (content.createTextRange && content.caretPos){
            var caretPos = content.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? val + ' ' : val;
        } else { content.value += val; }
        content.focus();
    }
</script>
";

            lnkInsertImage.Visible = true;
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "setvalue", js);
            string popup = ResolveUrl("~/portal/Galery/ImageGalery.aspx");
            lnkInsertImage.NavigateUrl = "javascript:popup2('" + popup + "', 500, 400);";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    if (!HttpContext.Current.User.Identity.IsAuthenticated)
                        Response.Redirect(UrlProvider.HomeUrl);

                    this.ViewState.Add("url", Request.UrlReferrer.PathAndQuery);

                    engine_forum engine = new engine_forum();
                    string replyID = Request.QueryString["ID"];
                    string content = engine.ReplyContent(replyID);
                    txtContent.Text = "[quote]" + content + "\r\n[/quote]";

                    RegistJavascriptEditor();
                    LoadStaffList();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            try
            {
                txtContent.Text = Utils.KillScript(txtContent.Text);

                if (txtContent.Text.Trim() == "")
                {
                    Response.Write("You have to write anything!");
                    return;
                }

                engine_forum engine = new engine_forum();
                int replyid = Convert.ToInt32(Request.QueryString["ID"]);
                int topicID = engine.TopicID(replyid);

                string username = HttpContext.Current.User.Identity.Name;
                if (vs.Authentication.Authenticate.CurrentUser.Staff)
                    username = ddlStaff.SelectedValue;
                engine.InsertReplyContent(txtContent.Text, topicID, username);
                DataCache.ResetPrefix(engine_xml.REPLY_LIST_CACHE_PREFIX);
                DataCache.RemoveCache(topic_lastest.CACHE_LATEST_TOPIC);

                Response.Redirect(this.ViewState["url"].ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}